/* eslint-disable no-unused-vars */
import React, { useEffect, useState } from "react";
import './launch.css'
import { useHistory } from "react-router-dom/cjs/react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { getUserState } from "@/store/actions/userState";
import icon_app_logo from "@/assets/icon_app_logo.png"
import { isLogin } from "@/utils/storage";
import { getAppConfig } from "@/store/actions/appConfig";
import { PageTag } from "@/utils/pageTag";
import { saveEvent } from "@/store/actions/saveEvent";
import { LangThai } from '@/utils/langThai';


/**
 * 启动页
 */
export default function Launch() {

    //判断是否登录
    const checkLogin = isLogin()
    console.log("checkLogin: " + checkLogin)

    // 获取路由历史 history 对象
    const history = useHistory()

    //获取 Redux 分发器
    const dispatch = useDispatch()

    //获取Redux Store中的app配置状态信息
    const appConfigInfo = useSelector(state => state.appConfig)

    //获取Redux Store中的用户状态信息
    const userStateInfo = useSelector(state => state.userState)

    //获取Redux Store中的用户状态信息
    const userNotExistInfo = useSelector(state => state.userNotExist)

    // 文案翻译
    const [curLang, setCurLang] = useState(LangThai);

    //获取app配置信息
    const getAppConfigInfo = () => {
        console.log("获取app配置信息=====")
        try {
            //获取Action
            const action = getAppConfig()

            //触发接口
            dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    //获取用户流程信息
    const getUserStateInfo = () => {
        console.log("获取用户流程信息=====")
        try {
            //获取Action
            const action = getUserState(PageTag.LAUNCH)

            //触发接口
            dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    //打点接口
    const doHitEvent = async (type, remark = "") => {
        console.log("doHitEvent: " + type + " , " + remark);

        try {
            //获取Action
            const action = saveEvent(type, remark)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    //启动时请求用户状态
    useEffect(() => {
        //获取时区
        let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        console.log("timeZone:" + timeZone);

        //打点
        doHitEvent("LAUNCH_START")

        //如果没有登录，直接转登录页
        setTimeout(() => {
            console.log("查询启动时请求用户状态：" + checkLogin)
            if (checkLogin) {
                //跳转首页
                history.replace("/home");
            } else {
                //跳转登录页
                history.replace("/register");
            }
        }, 2000)

        return () => {
            //打点
            doHitEvent("LAUNCH_END")
        }
    }, [])

    // useEffect(() => {
    //     if (appConfigInfo.status === APP_CONFIG_REQ_SUCCESS) {
    //         console.log("Launch-待等app配置信息返回")
    //         console.log(appConfigInfo)
    //         //获取用户状态
    //         getUserStateInfo()
    //     }
    // }, [appConfigInfo])

    // useEffect(() => {
    //     console.log("Launch-待等用户流程信息返回")
    //     console.log(userStateInfo)
    //     if (userStateInfo.status === LAUNCH_USER_STATE_REQ_SUCCESS) {
    //         console.log("Launch-再次查询user state:")
    //         console.log("Launch-applyStatus: " + userStateInfo.applyStatus)
    //         console.log("Launch-orderStatus: " + userStateInfo.orderStatus)

    //         //是否为审核中
    //         const isHomeReviewing = isLoanReviewing(userStateInfo.applyStatus)
    //         //是否为拒绝
    //         const isHomeReject = isLoanReject(userStateInfo.applyStatus, userStateInfo.orderStatus)
    //         //是否为放款中
    //         const isHomeLoaning = isLoanLoaning(userStateInfo.orderStatus)
    //         //是否为待还款
    //         const isHomeRepay = isLoanRepayNow(userStateInfo.orderStatus)
    //         //是否为待进件
    //         const isHomeNotLoan = ((!isHomeReviewing) && (!isHomeReject) && (!isHomeLoaning) && (!isHomeRepay))
    //         console.log("Launch-isHomeReviewing: " + isHomeReviewing)
    //         console.log("Launch-isHomeReject: " + isHomeReject)
    //         console.log("Launch-isHomeLoaning: " + isHomeLoaning)
    //         console.log("Launch-isHomeRepay: " + isHomeRepay)
    //         console.log("Launch-isHomeNotLoan: " + isHomeNotLoan)

    //         //页面转发
    //         setTimeout(() => {
    //             if (isHomeReviewing) {
    //                 history.replace('/review')
    //             } else if (isHomeReject) {
    //                 history.replace('/reject')
    //             } else if (isHomeLoaning) {
    //                 history.replace('/loaning')
    //             } else if (isHomeRepay) {
    //                 history.replace('/repay')
    //             } else {
    //                 history.replace('/home')
    //             }
    //         }, 500)
    //     }
    // }, [userStateInfo])

    //监听用户是否存在
    // useEffect(() => {
    //     if (userNotExistInfo.notExist === true) {
    //         history.replace('/register')
    //         // history.replace('/loginfb')
    //     }
    // }, [userNotExistInfo])


    return (
        <div className="launchRoot">
            <img
                className="launchImageSmall"
                src={icon_app_logo}
                alt="" />
            <span className="launchTitle">{curLang.appName}</span>
        </div>
    )
}